package com.wxd.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.wxd.dao.AccessDB;
import com.wxd.service.CarMapService;

public class CarMapServiceImpl implements CarMapService {
	public JSONArray getDevGroupList(String devName,String devType,String devUnit){
		StringBuffer whereStr = new StringBuffer();
		if(devName!=null){
			whereStr.append(" and 设备名称 like '%"+devName+"%'");
		}
		if(devType!=null){
			whereStr.append(" and 设备规格 like '%"+devType+"%'");
		}
		if(devUnit!=null){
			whereStr.append(" and 归属单位 like '%"+devUnit+"%'");
		}
		
		JSONArray result = new JSONArray();
		String sql = "select 设备编号,设备名称,设备规格,归属单位 from 运行信息 where 1=1"+whereStr.toString()+" group by 设备编号,设备名称,设备规格,归属单位";
		
		List queryForSql = AccessDB.queryForSql(sql, false);
		if(queryForSql!=null&&queryForSql.size()>0){
			for(int n=0;n<queryForSql.size();n++){
				Map map = (Map)queryForSql.get(n);
				
				JSONObject o1 = new JSONObject();
				o1.put("devNum", map.get(1).toString());
				o1.put("devName", map.get(2).toString());
				o1.put("devType", map.get(3).toString());
				o1.put("devUnit", map.get(4).toString());
				result.add(o1);
			}
		}
		return result;
	}
	
	public JSONArray getRecentDevInfoByDevNum(String devNum) {
		JSONArray result = new JSONArray();
		String sql = "select top 1 * from 运行信息 where 设备编号='"+devNum+"' order by 停止时间 desc";
		List queryForSql = AccessDB.queryForSql(sql, false);
		if(queryForSql!=null&&queryForSql.size()>0){
			Map map = (Map)queryForSql.get(0);
			
			JSONObject object = new JSONObject();
			object.put("id", map.get(1));
			object.put("devCode", map.get(2));
			object.put("devName", map.get(3));
			object.put("devSpi", map.get(4));
			object.put("ownUnit", map.get(5));
			object.put("tn", map.get(6));
			object.put("startTime", map.get(7).toString());
			object.put("endTime", map.get(8).toString());

			String runTime = map.get(9).toString();
			runTime = runTime.replaceAll("\\r", "");
			runTime = runTime.replaceAll("\\n", "");

			int runTimeNum = 0;
			try {
				runTimeNum = Integer.parseInt(runTime);
			} catch (Exception e) {
			}

			object.put("runTime", runTimeNum);
			object.put("runDistance", map.get(10)==null?"":map.get(10).toString());
			object.put("weidu", map.get(11)==null?"":map.get(11).toString());
			object.put("jingdu", map.get(12)==null?"":map.get(12).toString());
			object.put("youhao", map.get(13)==null?"":map.get(13).toString());
			object.put("luwen", map.get(14)==null?"":map.get(14).toString());
			result.add(object);
		}
		return result;
	}
	
	public JSONArray getTodaySummaryByDevNum(String devNum) {
		JSONArray result = new JSONArray();
		
		Calendar instance = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String startTime = sdf.format(instance.getTime());
		instance.set(Calendar.DATE, instance.get(Calendar.DATE) + 1);
		String endTime = sdf.format(instance.getTime());
		
		String sql = "select 设备编号,设备名称,设备规格,归属单位,sum(convert(float,运行时间)),sum(convert(float,运行里程)),sum(convert(integer,油耗)) from 运行信息 where 设备编号='"+devNum+"' and 停止时间<='"+endTime+"' and 停止时间>='"+startTime+"'  group by 设备编号,设备名称,设备规格,归属单位";
		
		List queryForSql = AccessDB.queryForSql(sql, false);
		if(queryForSql!=null&&queryForSql.size()>0){
			Map map = (Map)queryForSql.get(0);
			
			JSONObject object = new JSONObject();
			object.put("devCode", map.get(1));
			object.put("devName", map.get(2));
			object.put("devSpi", map.get(3));
			object.put("ownUnit", map.get(4));
			object.put("totalRunTime", map.get(5));
			object.put("totalDistance", map.get(6));
			object.put("totalYouhao", map.get(7));

			result.add(object);
		}
		return result;
	}
	
	public JSONArray getDevPointsByDevNum(String devNum, String startTime,
			String endTime) {
		JSONArray result = new JSONArray();
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
			Date parse = sdf.parse(endTime);
			Calendar instance = Calendar.getInstance();
			instance.setTime(parse);
			instance.set(Calendar.DATE, instance.get(Calendar.DATE) + 1);
			endTime = sdf.format(instance.getTime());
		}catch (Exception e) {
		}
		String sql = "select 当前维度,当前经度,停止时间 from 运行信息 where 设备编号='"+devNum+"' and 停止时间<='"+endTime+"' and 停止时间>='"+startTime+"' order by 停止时间 desc";
		List queryForSql = AccessDB.queryForSql(sql, false);
		if(queryForSql!=null&&queryForSql.size()>0){
			for(int n=0;n<queryForSql.size();n++){
				Map map = (Map)queryForSql.get(n);
				JSONObject obj = new JSONObject();
				obj.put("weidu", map.get(1)==null?"0":map.get(1).toString());
				obj.put("jingdu", map.get(2)==null?"0":map.get(2).toString());
				obj.put("time", map.get(3)==null?"0":map.get(3).toString());
				result.add(obj);
			}
		}
		return result;
	}
}
